<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item label="商品名称">
          <el-input v-model="form.name" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <!-- <el-form-item label="商品分类">
          <el-select
            v-model="form.type"
            placeholder="选择商品类型"
            @change="selectChange"
          >
            <el-option
              v-for="(item, index) in Data"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="商品分类">
          <el-cascader
            :options="options"
            :props="{
              emitPath: false,
              value: 'id',
              label: 'name',
              children: 'child',
            }"
            v-model="form.type"
          ></el-cascader>
        </el-form-item> -->
        <el-form-item label="商品类型" prop="type">
          <el-cascader
            v-model="form.type"
            :options="options"
            :props="{
              label: 'name',
              value: 'id',
              checkStrictly: true,
              emitPath: false,
            }"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="form.time"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
            @change="timeChange"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="primary" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="tableData"
        border
        style="width: 100%"
      >
        <el-table-column
          label="序号"
          type="index"
          :index="Method"
          prop="id"
          width="55"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="goodsName" align="center" label="商品名称">
        </el-table-column>
        <el-table-column prop="categoryName" align="center" label="商品分类">
        </el-table-column>
        <el-table-column prop="unit" align="center" label="商品单位">
        </el-table-column>
        <!-- <el-table-column align="center" label="水票图片">
          <template slot-scope="scope">
            <el-image
              style="width: 80px; height: 80px"
              :src="scope.row.water_img"
              fit="cover"
            ></el-image>
          </template>
        </el-table-column> -->
        <el-table-column prop="sell_price" align="center" label="商品单价">
        </el-table-column>
        <el-table-column prop="num" align="center" label="商品售卖数量">
        </el-table-column>
        <el-table-column prop="amount" align="center" label="售卖总价">
        </el-table-column>
        <el-table-column prop="sistime" align="center" label="统计日期">
          <!-- <template slot-scope="scope">
            {{
              scope.row.sistime
                .split(",")
                .map((date) => date.slice(0, 10))
                .join(",")
            }}
          </template> -->
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button @click="handleGo(scope.row)" size="mini" type="primary"
              >查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pageUser">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="packGit"
        border
        style="width: 100%"
      >
        <!-- <el-table-column
          label="序号"
          type="index"
          width="55"
          :index="Method"
          align="center"
        >
        </el-table-column> -->
        <el-table-column prop="order_id" align="center" label="订单编号">
        </el-table-column>
        <el-table-column prop="name" align="center" label="商品名称">
        </el-table-column>
        <!-- <el-table-column prop="ctime" align="center" label="下单日期">
        </el-table-column> -->
        <el-table-column prop="nickname" align="center" label="购买用户名称">
        </el-table-column>
        <el-table-column prop="mobile" align="center" label="用户手机号">
        </el-table-column>
        <el-table-column prop="nums" align="center" label="商品数量">
        </el-table-column>
        <el-table-column prop="amount" align="center" label="金额">
        </el-table-column>
      </el-table>
      <div class="pageUser">
        <el-pagination
          @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1"
          :current-page="page1"
          :page-sizes="[10, 150, 200, 300]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total1"
        >
        </el-pagination>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  packageCount,
  packageIndex,
  classification,
  goodSort,
  genreList,
} from "../../../../utils/api.js";
import {
  getFuSellingGoods,
  getFuSellingGoodsDetail,
} from "../../../../utils/main";
export default {
  data() {
    return {
      options: [],
      token: sessionStorage.getItem("token"),
      //请求特效
      loading: false,
      //弹窗
      dialogVisible: false,
      form: {
        name: "",
        type: "",
        time: "",
      },
      time: "",
      //发送请求时间参数
      //展示数据
      tableData: [],
      packGit: [],
      page: 1,
      limit: 10,
      total: 0,
      page1: 1,
      limit1: 10,
      total1: 0,
      name: "",
      type: "",
      order_id: -1,
      time: "",
      cat_id: "",
      goods_name: "",
      category_id: "",
      seller_id: sessionStorage.getItem("seller_id"),
      // seller_id: 6,
      goods_id: "",
    };
  },
  methods: {
    //日期
    timeChange(e) {
      // console.log(e);
      // let list = [];
      // e.forEach((item) => {
      //   let date = new Date(item);
      //   let year = date.getFullYear();
      //   let month = date.getMonth() + 1;
      //   let day = date.getDate();
      //   let h = date.getHours();
      //   let min = date.getMinutes();
      //   let s = date.getSeconds();
      //   month = month < 10 ? "0" + month : month;
      //   day = day < 10 ? "0" + day : day;
      //   h = h < 10 ? "0" + h : h;
      //   min = min < 10 ? "0" + min : min;
      //   s = s < 10 ? "0" + s : s;
      //   list.push(
      //     year + "-" + month + "-" + day + " " + h + ":" + min + ":" + s
      //   );
      // });

      // this.time = list[0] + "," + list[1];
      // console.log(this.time);
      this.time = this.form.time;
      if (this.form.time == null) {
        this.time = "";
      }
      this.getList();
    },
    //查询
    onSubmit() {
      // console.log(this.form);
      this.goods_name = this.form.name;
      this.time = this.form.time;
      this.category_id = this.form.type;
      this.page = 1;
      this.getList();
    },
    onReset() {
      this.goods_name = "";
      this.time = "";
      this.category_id = "";
      this.form.name = "";
      this.form.time = "";
      this.form.type = "";
      this.getList();
    },
    //水票分类
    // selectChange() {},
    async getSelect() {
      // this.loading = true;
      let type = "1";
      let res = await genreList(this.token)
        .then((res) => {
          // console.log(this.type, 88888);
          if (res.data.status) {
            let list = res.data.data;
            this.options = list.find((e) => {
              return e.name == "辅材采购";
            }).children;
            // console.log(this.options1);
            // this.total = res.data.data.count;
            // console.log(111, this.options);
            // let timer = setTimeout(() => {
            //   this.loading = false;
            //   clearTimeout(timer);
            // }, 400);
          } else {
            if (res.data.msg == "token失效,请重新进行登录") {
              this.$router.push("/materilsLogin");
            }
            if (res.data.msg == "此账号没有权限") {
              this.$message.error(res.data.msg);
              this.loading = false;
            }
            // let timer = setTimeout(() => {
            //   this.$message.error(res.data.msg);
            // }, 5000);
          }
        })
        .catch((err) => {
          console.log(err.message);
        });
    },
    //分页器
    handleSizeChange(val) {
      this.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getList();
    },
    handleSizeChange1(val) {
      this.limit1 = val;
      this.packageCounts(this.order_id);
      // this.packageIndexs();
    },
    handleCurrentChange1(val) {
      this.page1 = val;
      this.packageCounts(this.order_id);
    },
    // 序号排序
    Method(index) {
      if (this.total < this.limit) {
        return this.total - index;
      } else {
        return this.total - (this.page - 1) * this.limit - index;
      }
    },
    // 获取商品出售一览表列表
    async getList() {
      this.loading = true;
      let res = await getFuSellingGoods(
        this.token,
        this.seller_id,
        this.goods_name,
        this.category_id,
        this.time,
        this.page,
        this.limit
      );
      if (res.data.status) {
        this.tableData = res.data.data.data || [];
        this.total = res.data.data.count;
        // console.log(this.total, shouchu);
        let timer = setTimeout(() => {
          this.loading = false;
          clearTimeout(timer);
        }, 400);
      } else {
        if (res.data.msg == "token失效,请重新进行登录") {
          this.$router.push("/materilsLogin");
        }
        if (res.data.msg == "此账号没有权限") {
          this.$message.error(res.data.msg);
          this.loading = false;
        }
        let timer = setTimeout(() => {
          this.$message.error(res.data.msg);
        }, 5000);
      }
    },
    //获取列表数据
    async packageCounts() {
      let res = await getFuSellingGoodsDetail(
        this.token,
        this.goods_id,
        this.page1,
        this.limit1
      );
      if (res.data.status) {
        this.packGit = res.data.data.data || [];
        // this.tableData = res.data.data.data || [];
        this.packGit.forEach((item) => {
          item.allprice = item.nums * item.price;
        });
        this.total1 = res.data.data.count;
        // // console.log(this.total, shouchu);
        // let timer = setTimeout(() => {
        //   this.loading = false;
        //   clearTimeout(timer);
        // }, 400);
      } else {
        if (res.data.msg == "token失效,请重新进行登录") {
          this.$router.push("/materilsLogin");
        }
        if (res.data.msg == "此账号没有权限") {
          this.$message.error(res.data.msg);
          this.loading = false;
        }
        let timer = setTimeout(() => {
          this.$message.error(res.data.msg);
        }, 5000);
      }
    },
    // 点击详情的事件
    async handleGo(row) {
      this.dialogVisible = true;
      this.goods_id = row.goods_id;
      this.packageCounts();
      return;
      // this.$router.push(`/stn/goodsdetail?goods_id=${row.goods_id}`);
    },
    //关闭弹窗
    handleClose(done) {
      done();
      this.page1 = 1;
    },
  },
  mounted() {
    // this.sdhadj();
    this.getList();
    this.getSelect();
  },
};
</script>

<style scoped></style>